/**
 * 簿记行为交收详情
 * zhanghaitao
 */
import React, { PureComponent } from 'react';
import { connect } from 'dva';
import 'moment/locale/zh-cn';
import { DATA_ENTRY, ModalC, FormGroup, TradingClassif } from '../../../comps';
import moment from 'moment';
class behaviorDetail extends PureComponent {
  constructor(props) {
    super(props);
    this.state = {
      businessType: -1,
      instructionStatus: [
        { title: '已交收', value: '4' },
        { title: '未交收', value: '6' },
        { title: '已交收', value: '7' },
        { title: '到期交收', value: '8' },
        { title: '到期未交收', value: '11' },
      ],
    };
  }

  UNSAFE_componentWillMount() {
    const { detailObj } = this.props;
    this.setState({ businessType: detailObj.type });
  }

  //交收
  orderInstruction = () => {
    const { detailObj } = this.props;
    let data = this.getData.getFormGroupData();
    this.props.dispatch({
      type: 'bookkeepingBehaviorM/getOneInstruction',
      payload: {
        id: detailObj.id,
        settlementAmount: data.settlementAmount,
      },
      callback: (res) => {
        if (res === 'success') {
          this.props.changeVisible(false);
          this.props.refresh();
        }
      },
    });
  };

  //取消
  onCancel = () => {
    this.props.changeVisible(false);
  };

  // 模态框内容
  showTable = () => {
    const { detailObj = {} } = this.props;
    return (
      <FormGroup
        dataSource={[
          this.props.moreFunds
            ? {
                dataIndex: 'productGroup',
                type: DATA_ENTRY.PRODUCTGROUP,
                disabled: true,
                initialValue: detailObj.productGroup,
              }
            : {
                dataIndex: ['productId', 'portfolioInfoId'],
                title: ['产品', '组合'],
                type: DATA_ENTRY.PRODUCTGROUPSEP,
                disabled: true,
                initialValue: [detailObj.productId, detailObj.portfolioInfoId],
                multipleGroup: false,
              },
          {
            dataIndex: 'tradingcode',
            title: '证券代码',
            type: DATA_ENTRY.SECURITIESCODE,
            disabled: true,
            initialValue: `${detailObj.exchangecode}-${detailObj.tradingcode}`,
            codeType: 'positioned',
            queryItems: { securityType: '0,4,7,12,20,21' },
          },
          {
            dataIndex: 'exchangecode',
            title: '交易市场',
            type: DATA_ENTRY.TRADINGMARKET,
            disabled: true,
            initialValue: detailObj.exchangecode,
            marketType: ['jys', 'yhj', 'cw'],
          },
          {
            dataIndex: 'type',
            title: '资产类型',
            type: DATA_ENTRY.SELECT,
            dataSource: TradingClassif,
            disabled: true,
            initialValue: detailObj.type,
          },
          {
            dataIndex: 'direction',
            title: '交易方向',
            type: DATA_ENTRY.TRADINGDIRECT,
            businessType: this.state.businessType,
            disabled: true,
            initialValue: detailObj.type != '4' ? detailObj.direction : '回购到期',
            appType: '3',
          },
          {
            dataIndex: 'settlementAmount',
            title: '交易金额',
            type: DATA_ENTRY.INPUTNUMBER,
            //禁用规则：银行间现券和交易所现券的提前还本和到期不用改金额
            disabled:
              this.props.visible ||
              (detailObj.type == 0 && (detailObj.direction == 5 || detailObj.direction == 7)) ||
              (detailObj.type == 1 && (detailObj.direction == 5 || detailObj.direction == 7))
                ? true
                : false,
            initialValue: detailObj.settlementAmount,
            precision: 4,
            splitStyle: true,
            rules: [{ required: true, message: ' 请填写交易金额' }],
          },
          {
            dataIndex: 'settlementDate',
            title: '交收日期',
            type: DATA_ENTRY.SINGLEDATE,
            disabled: true,
            initialValue: detailObj.settlementDate ? moment(detailObj.settlementDate) : null,
          },
          {
            dataIndex: 'instructionStatus',
            title: '状态',
            type: DATA_ENTRY.SELECT,
            dataSource: this.state.instructionStatus,
            disabled: true,
            initialValue: detailObj.instructionStatus,
          },
        ]}
        rowNumber={2}
        autoLayout={false}
        onRef={(ref) => (this.getData = ref)}
      />
    );
  };

  render() {
    return (
      <ModalC
        title={this.props.visible ? '详情' : '交收'}
        visible={this.props.visible || this.props.visible1}
        maskClosable={false}
        footer={this.props.visible ? null : undefined}
        width={1200}
        onOk={this.props.visible ? null : this.orderInstruction}
        onCancel={this.onCancel}
        confirmLoading={this.props.loading.effects['bookkeepingBehaviorM/getTradeInstruction'] === true}
      >
        {this.showTable()}
      </ModalC>
    );
  }
}
export default connect(({ bookkeepingBehaviorM, loading }) => ({ bookkeepingBehaviorM, loading }))(behaviorDetail);
